import tkinter

# 定义窗口对象
window = tkinter.Tk()

# 设置窗口属性
window.title('我的第一个 GUI 程序')  # 标题
window.geometry('300x100+300+200')  # 位置及大小
# 格式：'wxh +-x +-y'，其中
# wxh：表示 宽x高（字母 xyz 的 x）
#  +x：表示距屏幕左边的距离；-x 表示距屏幕右边的距离
#  +y: 表示距屏幕上边的距离；-y 表示距屏幕下边的距离

# 主循环：窗口一直存在（除非被关闭）
window.mainloop()

####################################################################################################################################

from tkinter import  *
from tkinter import messagebox

class Application(Frame):
    def __init__(self,master=None):
        super().__init__(master) #父类的定义
        self.master=master
        self.pack()
        self.create_widget()
    def create_widget(self):
        ##创建组件
        self.label01=Label(self,text="用户名")
        self.label01.pack()

        user_name=StringVar()
        self.entry01=Entry(self,textvariable=user_name)
        self.entry01.pack()

        self.label02=Label(self,text="密码")
        self.label02.pack()

        password=StringVar()
        self.entry02=Entry(self,textvariable=password,show="*")
        self.entry02.pack()

        Button(self,text="登录",command=self.login).pack()

    def login(self):
        user_name=self.entry01.get()
        password=self.entry02.get()

        if user_name=='admin' and password=='123456':
            messagebox.showinfo("提示","登录成功！")
        else:
            messagebox.showinfo("提示","登录失败，请检查用户名或密码！")

if __name__=='__main__':
    window=Tk()
    window.title("一个经典的GUI程序类的测试")
    window.geometry("400x200+200+300")
    app=Application(master=window)
    window.mainloop()


    #########################################################################################################################

    from tkinter import *


class Application(Frame):
    def __init__(self, master=None):
        super().__init__(master)  # 父类的定义
        self.master = master
        self.pack()
        self.create_widget()

    def create_widget(self):
        """创建组件"""
        self.label01 = Label(self, text="标签1", width=10, height=2, bg="black", fg="white")
        self.label01.pack()

        self.label02 = Label(self, text="标签2", width=10, height=2, bg="blue", fg="white", font=("黑体", 20))
        self.label02.pack()

        # 显示文本
        self.label03 = Label(self, text="段落1\n段落段落2\n段落段落段落3", borderwidth=1, relief="solid", justify="right")
        self.label03.pack()

        # 显示图片
        global photo
        photo = PhotoImage(file="images/1.gif")  # 暂支持的图片格式：png、gif
        self.label04 = Label(self, image=photo)
        self.label04.pack()


if __name__ == '__main__':
    window = Tk()
    window.title("一个经典的GUI程序类的测试")
    window.geometry("400x300+200+300")
    app = Application(master=window)

    window.mainloop()

    ################################################################################################################################3

    from tkinter import *
from tkinter import messagebox


class Application(Frame):
    def __init__(self, master=None):
        super().__init__(master)  # 父类的定义
        self.master = master
        self.pack()
        self.create_widget()

    def create_widget(self):
        """创建组件"""
        self.text01 = Text(self, width=40, height=10, bg="gray")
        self.text01.pack()

        # 插入文本，格式：横坐标.纵坐标
        self.text01.insert(1.0, 'aaaaaaaaaaaaaaaa\nbbbbbbbbbbbbb')
        self.text01.insert(2.3, 'cccccccccccccccc\neeeeeeeeeeeee')

        Button(self, text="重复插入文本", command=self.insert_text).pack(side="left")

    def insert_text(self):
        self.text01.insert(INSERT, '鼠标光标处插入')
        self.text01.insert(END, '文本最后处插入')


if __name__ == '__main__':
    window = Tk()
    window.title("一个经典的GUI程序类的测试")
    window.geometry("400x200+200+300")
    app = Application(master=window)

    window.mainloop()

    ###############################################################################################################
    from tkinter import *
from tkinter import messagebox


class Application(Frame):
    def __init__(self, master=None):
        super().__init__(master)  # 父类的定义
        self.master = master
        self.pack()
        self.create_widget()

    def create_widget(self):
        """创建组件"""
        self.default = StringVar()
        self.default.set("F")
        rb1 = Radiobutton(self, text="男性", value="M", variable=self.default)
        rb2 = Radiobutton(self, text="女性", value="F", variable=self.default)

        rb1.pack(side="left")
        rb2.pack(side="left")

        Button(self, text="确定", command=self.confirm).pack()

    def confirm(self):
        messagebox.showinfo("提示", "选择的性别的是：" + self.default.get())


if __name__ == '__main__':
    window = Tk()
    window.title("一个经典的GUI程序类的测试")
    window.geometry("400x200+200+300")
    app = Application(master=window)

    window.mainloop()

############################################################################################################################################
from tkinter import *
from tkinter import messagebox


class Application(Frame):
    def __init__(self, master=None):
        super().__init__(master)  # 父类的定义
        self.master = master
        self.pack()
        self.create_widget()

    def create_widget(self):
        """创建组件"""
        canvas = Canvas(self, width=300, height=200)
        canvas.pack()
        # 画一条直线。以 横坐标,纵坐标 两个为一组
        canvas.create_line(10, 10, 30, 20, 40, 50)
        # 画一个矩形。
        canvas.create_rectangle(50, 50, 100, 100)
        # 画一个椭圆
        canvas.create_oval(50, 50, 100, 100)


if __name__ == '__main__':
    window = Tk()
    window.title("一个经典的GUI程序类的测试")
    window.geometry("400x200+200+300")
    app = Application(master=window)

    window.mainloop()


    
import tkinter as tk
 
def get_text_from_entry():
    print(entry.get())  # 获取文本框内容
 
root = tk.Tk()
root.title("文本框示例")
 
entry = tk.Entry(root)  # 创建文本框
entry.pack()
 
button = tk.Button(root, text="获取文本", command=get_text_from_entry)  # 创建按钮，点击时获取文本框内容
button.pack()
 
root.mainloop()

###########################################################################################################################################
import tkinter as tk
from tkinter import messagebox
 
def modify_content():
    v.set("张博是帅哥")
    # 获取当前编辑框的内容
    #original_content = entry.get()
    # 修改内容
    #entry.delete(0, tk.END)  # 清空编辑框
    #entry.insert(0, "新内容")  # 插入新内容
    # 弹窗显示原始内容
    #tk.messagebox.showinfo("原始内容", original_content)
    tk.messagebox.showinfo("文本内容", v.get())
 
root = tk.Tk()
root.title("编辑框内容修改")
 
# 创建一个标签和编辑框
label = tk.Label(root, text="请输入内容：")
label.pack()
 
#entry = tk.Entry(root)
#entry.pack()


v=tk.StringVar()
e=tk.Entry(root,textvariable=v)
e.pack()
v.set("I love Python!")
s=v.get()
 
# 创建一个按钮，点击后修改编辑框内容
button = tk.Button(root, text="修改内容", command=modify_content)
button.pack()
 
root.mainloop()


import tkinter as tk

master=tk.Tk()

tk.Label(master,text="作品：").grid(row=0)
tk.Label(master,text="作者：").grid(row=1)

e1=tk.Entry(master)
e2=tk.Entry(master)
e1.grid(row=0,column=1,padx=10,pady=5)
e2.grid(row=1,column=1,padx=10,pady=5)

def show():
	print("作品:《%s》"%e1.get())
	print("作者:《%s》"%e2.get())
	e1.delete(0,"end")
	e2.delete(0,"end")

tk.Button(master,text="获取信息",width=10,command=show).grid(row=3,column=0,sticky="w",padx=10,pady=5)
tk.Button(master,text="退出",width=10,command=master.quit).grid(row=3,column=1,sticky="e",padx=10,pady=5)

master.mainloop()



